package it.csi.finregio.finreges.util;

import java.text.MessageFormat;
import java.util.ResourceBundle;

import it.csi.finregio.finreges.dto.DestinatarioScheda;
import it.csi.finregio.finreges.dto.EnteScheda;
import it.csi.finregio.finreges.dto.NotaScadenza;
import it.csi.finregio.finreges.dto.Riferimento;
import it.csi.finregio.finreges.dto.Scheda;
import it.csi.finregio.finreges.presentation.form.SchedaForm;

public class SchedaData {

	private Scheda scheda;
	private Riferimento[] rif;
	private DestinatarioScheda destinatario;
	private NotaScadenza notaScadenza;
	private EnteScheda[] entiScheda;

	private ResourceBundle bundle = ResourceBundle.getBundle("finreges");

	public SchedaData(Scheda scheda, Riferimento[] riferimentos,
			DestinatarioScheda destinatario, NotaScadenza notaScadenza,
			EnteScheda[] enteSchedas) {
		this.scheda = scheda;
		this.rif = riferimentos;
		this.destinatario = destinatario;
		this.notaScadenza = notaScadenza;
		this.entiScheda = enteSchedas;
	}

	public EnteScheda[] getEntiScheda() {
		return entiScheda;
	}

	public Scheda getScheda() {
		return scheda;
	}

	public Riferimento[] getRif() {
		return rif;
	}

	public DestinatarioScheda getDestinatario() {
		return destinatario;
	}

	public NotaScadenza getNotaScadenza() {
		return notaScadenza;
	}

	/**
	 * Restituisce la classe in formato html
	 */
	public String toString() {
		StringBuffer sb = new StringBuffer();
		String header = /*
						 * "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0
						 * Transitional//EN\"
						 * \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" + "<html
						 * xmlns=\"http://www.w3.org/1999/xhtml\">" + "<body>" +
						 */"<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
		String footer = "</table><br>";// "</table></body></html>";
		sb.append(header);
		String tableHeader = bundle.getString("HEADER");
		String tableValue = bundle.getString("VALUE");
		String openTR = "<tr>";
		String closeTR = "</tr>";
		sb.append(openTR);
		formatProperty(sb, tableHeader, "Titolo");
		formatProperty(sb, tableValue, scheda.getTitolo());
		sb.append(closeTR);

		if (getRif() != null && getRif().length != 0) {
			sb.append(openTR);
			formatProperty(sb, tableHeader, "Riferimenti normativi");
			StringBuffer rifNor = new StringBuffer();
			for (int i = 0; i < getRif().length; i++) {
				Riferimento rif = getRif()[i];
				rifNor.append(rif.getDescrizioneEstesa()).append("<br>");

			}
			formatProperty(sb, tableValue, rifNor.toString());
			sb.append(closeTR);
		}

		sb.append(openTR);
		formatProperty(sb, tableHeader, "Materia");
		formatProperty(sb, tableValue, scheda.getClassificazione()
				.getDescrizione());
		sb.append(closeTR);

		if (destinatario != null) {
			if (destinatario.getDestinatari() != null
					&& !destinatario.getDestinatari().equals("")) {
				sb.append(openTR);
				formatProperty(sb, tableHeader, "Destinatari");
				formatProperty(sb, tableValue, destinatario.getDestinatari());
				sb.append(closeTR);
			}
			if (destinatario.getProcedura() != null
					&& !destinatario.getProcedura().equals("")) {
				sb.append(openTR);
				formatProperty(sb, tableHeader, "Procedura");
				formatProperty(sb, tableValue, destinatario.getProcedura());
				sb.append(closeTR);
			}
			if (destinatario.getFinanziamenti() != null
					&& !destinatario.getFinanziamenti().equals("")) {
				sb.append(openTR);
				formatProperty(sb, tableHeader, "Finanziamenti");
				formatProperty(sb, tableValue, destinatario.getFinanziamenti());
				sb.append(closeTR);
			}
		}

		if (notaScadenza != null) {
			sb.append(openTR);
			formatProperty(sb, tableHeader, "Scadenza");
			if (notaScadenza.getDataScadenza() != null) {
				formatProperty(sb, tableValue, notaScadenza.getDataScadenza());
			} else {
				formatProperty(sb, tableValue, notaScadenza
						.getTermineScadenza());
			}
			sb.append(closeTR);
		}

		if (getEntiScheda() != null && getEntiScheda().length != 0) {
			// prima faccio STANDARD E NON STANDARD, POI ALTRI ENTI
			EnteScheda[] es = getEntiScheda();
			formatAssessorati(sb, tableHeader, tableValue, openTR, closeTR, es);
			formatAltriEnti(sb, tableHeader, tableValue, openTR, closeTR, es);

		}
		return sb.append(footer).toString();
	}

	/**
	 * @param sb
	 * @param tableHeader
	 * @param tableValue
	 * @param openTR
	 * @param closeTR
	 * @param es
	 */
	private void formatAltriEnti(StringBuffer sb, String tableHeader,
			String tableValue, String openTR, String closeTR, EnteScheda[] es) {
		StringBuffer enti = new StringBuffer();
		boolean add = false;
		for (int i = 0; i < es.length; i++) {
			EnteScheda e = es[i];
			StringBuffer sbEmail = new StringBuffer("Email: ");
			StringBuffer sbWebsite = new StringBuffer("Sito web: ");
			boolean addEmail = false;
			boolean addSite = false;
			if (Integer.parseInt(e.getModalitaInserimento()) == SchedaForm.ALTRI_ENTI) {
				enti.append(e.getDescrizioneEstesa()).append("<br>");
				add=true;
				if (e.getEmail1() != null && !e.getEmail1().equals("")) {
					addEmail=true;
					sbEmail.append(e.getEmail1()).append(",");
				}
				if (e.getEmail2() != null && !e.getEmail2().equals("")) {
					addEmail=true;
					sbEmail.append(e.getEmail2()).append(",");
				}
				if (e.getEmail3() != null && !e.getEmail3().equals("")) {
					addEmail=true;
					sbEmail.append(e.getEmail3()).append(",");
				}
				if (sbEmail.toString().endsWith(",")) {
					sbEmail = new StringBuffer(sbEmail.substring(0, sbEmail
							.toString().lastIndexOf(",")));
				}
				if (e.getWebsite1() != null && !e.getWebsite1().equals("")) {
					addSite=true;
					sbWebsite.append(e.getWebsite1()).append(",");
				}
				if (e.getWebsite2() != null && !e.getWebsite2().equals("")) {
					addSite=true;
					sbWebsite.append(e.getWebsite2()).append(",");
				}
				if (e.getWebsite3() != null && !e.getWebsite3().equals("")) {
					addSite=true;
					sbWebsite.append(e.getWebsite3()).append(",");
				}
				if (sbWebsite.toString().endsWith(",")) {
					sbWebsite = new StringBuffer(sbWebsite.substring(0,
							sbWebsite.toString().lastIndexOf(",")));
				}
				enti.append(addEmail ?  sbEmail.toString() : "");
				enti.append(addSite? sbWebsite.toString() : "");
			}
		}
		if (add) {
			sb.append(openTR);
			formatProperty(sb, tableHeader, "Ente");
			formatProperty(sb, tableValue, enti.toString());
			sb.append(closeTR);
		}
	}

	/**
	 * @param sb
	 * @param tableHeader
	 * @param tableValue
	 * @param openTR
	 * @param closeTR
	 * @param es
	 */
	private void formatAssessorati(StringBuffer sb, String tableHeader,
			String tableValue, String openTR, String closeTR, EnteScheda[] es) {
		StringBuffer assessorati = new StringBuffer();
		boolean add = false;
		for (int i = 0; i < es.length; i++) {
			EnteScheda e = es[i];
			boolean addEmail = false;
			boolean addSite = false;

			StringBuffer sbEmail = new StringBuffer("Email: ");
			StringBuffer sbWebsite = new StringBuffer("Sito web: ");
			if (Integer.parseInt(e.getModalitaInserimento()) == SchedaForm.STANDARD
					|| Integer.parseInt(e.getModalitaInserimento()) == SchedaForm.NON_STANDARD) {
				assessorati.append(e.getDescrizioneEstesa()).append("<br>");
				add = true;
				if (e.getEmail1() != null && !e.getEmail1().equals("")) {
					addEmail=true;
					sbEmail.append(e.getEmail1()).append(",");
				}
				if (e.getEmail2() != null && !e.getEmail2().equals("")) {
					addEmail=true;
					sbEmail.append(e.getEmail2()).append(",");
				}
				if (e.getEmail3() != null && !e.getEmail3().equals("")) {
					addEmail=true;
					sbEmail.append(e.getEmail3()).append(",");
				}
				if (sbEmail.toString().endsWith(",")) {
					sbEmail = new StringBuffer(sbEmail.substring(0, sbEmail
							.toString().lastIndexOf(",")));
				}
				if (e.getWebsite1() != null && !e.getWebsite1().equals("")) {
					addSite=true;
					sbWebsite.append(e.getWebsite1()).append(",");
				}
				if (e.getWebsite2() != null && !e.getWebsite2().equals("")) {
					addSite=true;
					sbWebsite.append(e.getWebsite2()).append(",");
				}
				if (e.getWebsite3() != null && !e.getWebsite3().equals("")) {
					addSite=true;
					sbWebsite.append(e.getWebsite3()).append(",");
				}
				if (sbWebsite.toString().endsWith(",")) {
					sbWebsite = new StringBuffer(sbWebsite.substring(0,
							sbWebsite.toString().lastIndexOf(",")));
				}
				assessorati.append(addEmail?sbEmail.toString() : "");
				assessorati.append(addSite?sbWebsite.toString() : "");
			}
		}
		if (add) {
			sb.append(openTR);
			formatProperty(sb, tableHeader, "Assessorato");
			formatProperty(sb, tableValue, assessorati.toString());
			sb.append(closeTR);
		}
	}

	/**
	 * @param sb
	 * @param tableHeader
	 * @param header
	 */
	private void formatProperty(StringBuffer sb, String pattern, String value) {
		sb.append(MessageFormat.format(pattern, new Object[] { value }));
	}
}
